/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "bus_center.h"
#include "common.h"
#include "discover.h"
#include "transmission.h"

#define CMD_DESC_INFO_LEN 32
#define NET_WORKOUT_ID_MAX 10

typedef void(*CmdFunc)(void);
typedef struct {
    char desc[CMD_DESC_INFO_LEN];
    CmdFunc func;
} CmdNode;

bool g_isServer = false;

#ifdef RICH_SYSTEM
void AddPermission();
#endif

int main(int argc, char **argv)
{
    char device_num = 0;
    static DeviceList deviceList = { 0, {0} };
    NetworkIdlist g_workoutidList = {0};

    if (argc <= 1) {
        printf("param error,please enter softbus_tool server&softbus_tool client\n");
        return -1;
    }

    if (strncmp(argv[1], "server", 5) == 0) {
        printf("softbus_tool server start\n");
        g_isServer = true;
    } else {
        g_isServer = false;
        printf( "softbus_tool client start\n");
    }

#ifdef RICH_SYSTEM
    AddPermission();
#endif

    if (g_isServer) {
        /* Start discovery for server */
        D_StartDiscovery();
    } else {
        D_PublishService();
    }

    T_CreateSessionServer();
    while (true) {
        if (GetJoinLnnFlag() == true) {
            ConnectionAddr addr;
            (void)memset_s(&addr, sizeof(ConnectionAddr), 0, sizeof(ConnectionAddr));
            printf("JoinLnnFlag is true\n");
            
            GetJoinLnndata(&deviceList);
            
            for (int i = 0; i < deviceList.num; i++) {
                for (int k = 0; k < deviceList.dev->addrNum; k++) {
                    addr.type = CONNECTION_ADDR_ETH;
                    addr.info.ip.port = deviceList.dev[i].addr[k].info.ip.port;
                    memcpy(addr.info.ip.ip, deviceList.dev[i].addr[k].info.ip.ip, strlen(deviceList.dev[i].addr[k].info.ip.ip));
                    int ret = BC_JoinLNN(&addr);
                    if (ret != 0) {
                        break;
                    }
                    device_num ++;
                }
                (void)memset_s(&addr, sizeof(ConnectionAddr), 0, sizeof(ConnectionAddr));
            }
            ClearFoundDevices();
            SetJoinLnnFlag(false);
        }
        if (device_num != 0) {
            GetNetworkIdList(&g_workoutidList);
            for (int i = 0; i < device_num; i++) {
                int sessionId = T_OpenSession(&g_workoutidList.id[i]);
                if (sessionId > 0) {
                    T_SendBytes(sessionId);
                }
            }
            memset_s(&g_workoutidList, sizeof(NetworkIdlist), 0, sizeof(NetworkIdlist));
            SetNetworkIdList(&g_workoutidList);
            device_num = 0;
        }
    }
}
